package com.javadroid.webservices;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.javadroid.model.CapituloModel;
import com.javadroid.model.Comutador;
import com.javadroid.model.NovelaModel;
import com.javadroid.sgbd.CapituloDao;
import com.javadroid.sgbd.MysqlCargaInicial;
import com.javadroid.sgbd.NovelaDao;
import com.javadroid.webget.ConnectorSite;

@Path("getNovelas")
public class WebServiceServer {


	@GET
	@Produces("application/json")
    public Response getListaNovelasAtivas(){
    	NovelaDao dao = new NovelaDao();
    	List<NovelaModel> novelas = (List<NovelaModel>) dao.getListaNovelas();
    	return Response.status(200).entity(montaJsonFormato(novelas)).build();
    }
    
    @GET
    @Path("/allCaps")
	@Produces("application/json")
    public Response getListaTodosCapitulosDaSemana(){
    	CapituloDao dao = new CapituloDao();
    	List<CapituloModel> novelas =  (List<CapituloModel>) dao.getCapitulosTodasNovela();
    	return Response.status(200).entity(montaJsonFormato(novelas)).build();
    }  

    
    @GET
    @Path("/montaBandoDeDados")
    public Response montaBancoDeDados(){
		MysqlCargaInicial.tabelas();
		MysqlCargaInicial.primeiraCarga();
		return Response.status(200).entity("Estrutura inicial montada").build();		
    }
    
    
    @POST
    @Path("/updateEmergencial")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateEmergencial(String data){
    	
    	JsonParser parser = new JsonParser();
		String sql = null;

    	try {
			Object obj = parser.parse(data);
			JsonObject jsonObj = (JsonObject) obj;
			sql = jsonObj.get("sql").getAsString();
			
		} catch (Exception e) {
			e.getMessage();
		}    	
    	
		boolean result = MysqlCargaInicial.updateEmergencial(sql);
		return Response.status(200).entity(result ? "Query Executada com sucesso" : "Erro na execucao da Query").build();		
		
    }
    
    
    @POST
    @Path("/postone")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response getCapitulosDeUmaNovela(String data){
    	
    	JsonParser parser = new JsonParser();
		JsonElement nov = null;
		JsonElement maxNov = null;
		JsonElement cap = null;
    	try {
			Object obj = parser.parse(data);
			JsonObject jsonObj = (JsonObject) obj;
			nov = jsonObj.get("id_nov");
			cap = jsonObj.get("max_cap");
			maxNov = jsonObj.get("max_nov");
			
		} catch (Exception e) {
			e.getMessage();
		}
		
    	// buscando informacao do banco
    	CapituloDao dao = new CapituloDao();
    	if(cap != null && nov != null){
    		Comutador comutador = dao.comutaJson(nov.getAsInt(), cap.getAsInt(), maxNov.getAsInt() );
    		String result = montaJsonFormato(comutador);
    		return Response.status(200).entity(result).build();
    	}
    	return null;

	}
    
    @POST
    @Path("/cargaDeDados")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response cargaDeDados(String data){
    	ConnectorSite con = new ConnectorSite();
    	con.atualizaBancoDeDadosDeTodasAsNovelas();
		return Response.status(200).entity("Estrutura de Dados montada").build();		
    } 
    
    @POST
    @Path("/cargaDeDadosPorNovela")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response cargaDeDadosPorNovela(String data){
    	
    	JsonParser parser = new JsonParser();
		JsonElement nov = null;

		try {
			Object obj = parser.parse(data);
			JsonObject jsonObj = (JsonObject) obj;
			nov = jsonObj.get("id_nov");
		} catch (Exception e) {
			e.getMessage();
		}
    	
    	ConnectorSite con = new ConnectorSite();
    	con.atualizaBancoDeUmaNovela(nov.getAsInt());
		return Response.status(200).entity("ok").build();		
    }
    
    public String montaJsonFormato(Object obj){
    	GsonBuilder buildJson = new GsonBuilder();
    	Gson json = buildJson.create();
    	return json.toJson(obj);
    	
    }
	
	public Object montaObjectJson(String json,Class<?> entity){
		GsonBuilder builder = new GsonBuilder();
		Gson gson = builder.create();
		Object obj = gson.fromJson(json, entity);
		return obj;
	}
	
	@POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createDataInJSON(String data) { 
        String result = "Data post: "+data;
        return Response.status(201).entity(result).build(); 
    }
	
}
